import Vue from 'vue';
import {
  getBusinessProjectType,
  getProjectMainList,
  getProjectMainCount
} from '@/api/project'

const statusMap = {
  0: ['1', '2', '3'],
  1: ['21', '22', '23', '24'],
  2: ['31', '32', '33'],
  3: ['41', '42', '43', '44', '45'],
  4: ['51'],
}

const project = {
  state: {
    testPicUrls: JSON.parse('[{"name":"Ju5hbSI0U6Rvdef5b0c50ff99836bb37c2c598bff95f.jpg","url":"https://saving.jiayao.live/hzjn/2025/09/26/b4cc5f7869974952afb8c2210f738214.jpg"},{"name":"0QNGRYd58Rp1b3032023431ed279023ff6dcbbdfac03.jpg","url":"https://saving.jiayao.live/hzjn/2025/09/26/531e5ce1d3a84177b54f137d1be6bf5c.jpg"},{"name":"WSx1KM8HsR_j6cc65cff50e0f294a91e8d72b8cce1d7.jpg","url":"https://saving.jiayao.live/hzjn/2025/09/26/07c5888ddf9f4e749e6813082369090d.jpg"}]'),
    businessProjectTypeList: [],
    businessOppStatusList: [
      {
        id: '1',
        count: 0
      },
      {
        id: '2',
        count: 0
      },
      {
        id: '3',
        count: 0
      }
    ],
    // 项目测电
    testSolutionEditTarget: null, // 测电方案编辑对象
    ProjectMainCount: null, //商机，测电等的status状态总数
    activeTab: '',
    statusCountMap: {},
    targetInfo: {}, // 项目的基本信息
  },
  getters: {
    businessOppSumCount: state => {
      return state.businessOppStatusList.reduce((sum, item) => {
        return sum + item.count
      }, 0)
    }
  },

  mutations: {
    SET_BusinessProjectTypeList: (state, data) => {
      state.businessProjectTypeList = data
    },
    SET_BusinessOppStatusList: (state, [id, count]) => {
      console.log(id, count, '1')
      state.businessOppStatusList.forEach(item => {
        if (item.id === id) {
          item.count = count
        }
      })
    },
    SET_TestSolutionEditTarget: (state, data) => {
      state.testSolutionEditTarget = data
    },
    SET_ProjectMainCount: (state, data) => {
      state.ProjectMainCount = data
    },
    SET_ActiveTab: (state, data) => {
      state.activeTab = data
    },
    SET_StatusCountMap: (state, [key, data]) => {
      Vue.set(state.statusCountMap, key, data);
    },
    SET_TargetInfo: (state, data) => {
      state.targetInfo = data
    }
  },

  actions: {
    async getProjectMainCount({ commit }, type) {
      const res = await getProjectMainCount(null)
      const { business, construction, contract, group5, testelec } = res.data
      commit('SET_ProjectMainCount', {
        business,
        construction,
        contract,
        group5,
        testelec
      })
    },
    // 获取项目下商机、测电、合同等其中一个的各个status状态数
    async getProjectStatusCount({ state, commit }, activeTab) {
      commit('SET_ActiveTab', activeTab)
      const res = await getProjectMainCount(statusMap[activeTab][0])
      console.log(res)
      const list = statusMap[activeTab].map(item => {
        return {
          status: item,
          count: res.data.find(item2 => String(item2.status) === item)?.count || '0'
        }
      })
      commit('SET_StatusCountMap', [activeTab, list])
      console.log(state.statusCountMap)
    },
    GetBusinessProjectTypeList({ commit }, type) {
      return new Promise((resolve, reject) => {
        getBusinessProjectType(type).then(res => {
          console.log(res, 'GetBusinessProjectTypeList----')
          const list = (res.data || []).map(item => {
            return {
              text: item.dictLabel,
              value: item.dictLabel,
              dictValue: item.dictValue
            }
          })
          commit('SET_BusinessProjectTypeList', list)
          resolve(list)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取项目商机的总数量以及三种状态数量
    GetProjectMainCount({ commit, state }) {
      return new Promise((resolve, reject) => {
        Promise.all(state.businessOppStatusList.map(async item => {
          return getProjectMainList({
            status: item.id,
            pageNum: 1,
            pageSize: 1,
          }).then(res => {
            if (res.code === 200) {
              commit('SET_BusinessOppStatusList', [item.id, res.total])
            }
          })
        })).then(() => {
          resolve(state.businessOppStatusList)
        })
      })

    },
  }
}

export default project
